package com.wing.user.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.user.model.entity.SelContactInfo;
import com.wing.user.model.entity.WarehouseUser;
import com.wing.user.model.response.SelContactInfoAuthVO;
import com.wing.user.model.response.SelContactInfoVO;
import com.wing.user.service.SelContactInfoService;
import com.wing.user.service.WarehouseMenuService;
import com.wing.user.service.WarehouseUserService;
import com.wing.web.annotation.AuthPass;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 客户端-Controller
 *
 * @author
 */
@Api(value = "云仓", tags = "#/warehouse/user")
@RestController("WarehouseUserController")
@RequestMapping("/warehouse/admin/user")
@RequiredArgsConstructor
@Slf4j
public class WarehouseUserController {

    private final WarehouseUserService warehouseUserService;
    private final WarehouseMenuService warehouseMenuService;
    private final SelContactInfoService contactInfoService;


    @ApiOperation(value = "查询用户", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String", paramType = "form")
    })
    @GetMapping("/getByUsername")
    public JsonResult<SelContactInfoAuthVO> getByUsername(String account){
        SelContactInfoAuthVO vo = contactInfoService.getByUsername(account);
        return JsonResult.success(vo);
    }

    @ResponseBody
    @ApiOperation(value = "获取所有菜单")
    @ApiImplicitParam(name = "access_token", value = "令牌", required = true, dataType = "String", paramType = "query")
    @GetMapping("/menu")
    public JsonResult userMenu() {
        return JsonResult.success(warehouseMenuService.getMenuListByUserId(JwtUtils.getLoginUserId()));
    }

    @ApiOperation(value = "查询用户", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String", paramType = "form")
    })
    @GetMapping("/list")
    public PageResult<WarehouseUser> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "desc") String order
            ,String warehouseCustomerId
    ){

        Page<WarehouseUser> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        if(StrUtil.isNotBlank(sort)){
            paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        }
        if(StrUtil.isNotBlank(order)){
            paramMap.put("order",StrUtil.toUnderlineCase(order));
        }
        if(StrUtil.isNotBlank(warehouseCustomerId)){
            paramMap.put("warehouseCustomerId",StrUtil.toUnderlineCase(warehouseCustomerId));
        }

        Page<WarehouseUser> list = warehouseUserService.pageList4app(pg, paramMap);

        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加用户本信息", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@RequestBody WarehouseUser warehouseUser) {
        if (warehouseUserService.add(warehouseUser) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    @ApiOperation(value = "修改用户基本信息", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@RequestBody WarehouseUser warehouseUser) {
        return warehouseUserService.updateWarehouseUser(warehouseUser);
    }

    @ApiOperation(value = "删除用户基本信息", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warehouseUserId", value = "用户基本信息id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long warehouseUserId) {
        return warehouseUserService.deleteWarehouseUser(warehouseUserId);
    }

    @AuthPass
    @ResponseBody
    @ApiOperation(value = "获取个人信息")
    @ApiImplicitParam(name = "access_token", value = "令牌", required = true, dataType = "String", paramType = "query")
    @GetMapping("/userInfo")
    public JsonResult userInfo() {
        log.error("userInfo--->",JwtUtils.getJwtPayload().toString());
        System.out.println("userInfo--->"+JwtUtils.getJwtPayload().toString());
        SelContactInfo user = contactInfoService.getById(JwtUtils.getLoginUserId());
        user.setPassword("");
        SelContactInfoVO selUser = new SelContactInfoVO();
        BeanUtil.copyProperties(user, selUser);
        selUser.setClientId("warehouse");
        return JsonResult.success(selUser);
    }
}
